package org.itboys.core.service.mongo.web;

import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.tray.SundayTrayModelMapper;
import org.itboys.core.dro.order.SundaySendOrderDro;
import org.itboys.core.dro.order.SundaySendOrderModelDro;
import org.itboys.core.entity.mongo.SundayOrderFailRecord;
import org.itboys.core.entity.mysql.tray.SundayTrayModel;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作者：zzq
 * 时间：2019/1/9
 * 描述：订单失败记录
 */
@Service
public class SundayOrderFailRecordService extends BaseMongoService<SundayOrderFailRecord> {
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Autowired
    private SundayTrayModelMapper modelMapper;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayOrderFailRecord> getEntityClass() {
        return SundayOrderFailRecord.class;
    }

    /**
     * 新增失败记录
     *
     * @param orderDro
     */
    public void save(Long memberId, String mobile, SundaySendOrderDro orderDro) {
        SundayOrderFailRecord record = new SundayOrderFailRecord(memberId, mobile, orderDro.getType(), orderDro.getTime(), orderDro.getGetType(),
                orderDro.getGetTime(), orderDro.getSendProvince(), orderDro.getSendCity(), orderDro.getSendDistrict(),
                orderDro.getSendAddress(), orderDro.getRecycleProvince(), orderDro.getRecycleCity(),
                orderDro.getRecycleDistrict(), orderDro.getRecycleAddress(),SundayCommonConstants.NO);
        String modelMsg = "";
        List<SundaySendOrderModelDro> orderModels = orderDro.getModels();
        List<Long> modelIds = orderModels.stream().map(e -> e.getModelId()).collect(Collectors.toList());
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("ids", modelIds);
        List<SundayTrayModel> models = modelMapper.select(param);
        for (SundaySendOrderModelDro modelDro : orderModels) {
            for (SundayTrayModel model : models) {
                if (model.getId().longValue() == modelDro.getModelId().longValue()) {
                    modelMsg += model.getName() + "X" + modelDro.getNum();
                    break;
                }
            }
        }
        record.setModelMsg(modelMsg);
        super.save(record);
    }


    public PageResult<SundayOrderFailRecord> select(HttpServletRequest request) {
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request, "status");
        param.put("isDeleted", 0);
        Map<String, String> containsparam = ParamUtil.packageMongoVagueParam(request, "");
        PageResult<SundayOrderFailRecord> result = super.containpageQuery(request, containsparam, param);
        return result;
    }

    /**
     * 标记通过
     * @param recordId
     */
    public void handle(Long recordId){
        SundayOrderFailRecord record = super.getById(recordId);
        record.setStatus(SundayCommonConstants.YES);
        super.updateExceptEmpty(recordId, record);
    }


}
